﻿using System;
using System.Windows.Forms;
using System.Xml;

namespace RibbonEditor.Forms.CommandDefinitions
{
    public partial class CommandSelectionPicker : Form
    {
        public string SelectedCommandDefinition { get; set; }

        public CommandSelectionPicker(XmlNode commandDefinitions)
        {
            InitializeComponent();
            this.FillList(commandDefinitions);
        }

        private void FillList(XmlNode commandDefinitions)
        {
            this.lstItems.Items.Clear();

            foreach (XmlNode commandDefinitionNode in commandDefinitions.ChildNodes)
            {
                if (commandDefinitionNode.Attributes["Id"] != null)
                {
                    this.lstItems.Items.Add(commandDefinitionNode.Attributes["Id"].Value);
                }
                else
                {
                    this.lstItems.Items.Clear();
                    MessageBox.Show(this, "At least one command definition has no Id. Please check command definitions before using this dialog", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.DialogResult = DialogResult.Cancel; 
                    this.Close();
                }
            }
        }

        private void btnCommandDefSelectionValidate_Click(object sender, EventArgs e)
        {
            if (this.lstItems.SelectedItems.Count == 0)
            {
                MessageBox.Show(this, "Please select a command definition in the list", "Information", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                this.SelectedCommandDefinition = this.lstItems.SelectedItems[0].Text;

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void btnCommandDefSelectionCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void lstItems_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.lstItems.SelectedItems.Count == 0)
            {
                MessageBox.Show(this, "Please select a command definition in the list", "Information", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                this.SelectedCommandDefinition = this.lstItems.SelectedItems[0].Text;

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }
    }
}
